module SmtpcomApi
  class Account < Client
    ACCESSORS  = [:company_name, :phone, :last_name, :first_name]
    READERS    = [:email, :prepaid_quota, :reset_quota_at, :ip_pool, :usage]
    ATTRIBUTES = READERS + ACCESSORS

    include ::SmtpcomApi::Helpers

    attr_accessor *ACCESSORS
    attr_reader   *READERS

    def initialize(api_key, api_host)
      super
      reload
    end

    def reload
      set_attributes get(:account)['account']
      self
    end

    def dashboard
      st = @reset_quota_at.to_i>0 ? @reset_quota_at - 30*24*24 : now.to_i - 30*24*60*60
      get(:dashboard, :time_end => now.to_i, :time_start => st)['dashboard']
    end

    def save
      update = attributes.select{|k| ACCESSORS.include? k}
      put :account, :account => update
      self
    end

  end
end